package com.info.method.controller;

import com.info.method.entity.Kzpcsxxb;
import com.info.method.entity.Kztyzbxxb;
import com.info.method.service.LeveyJenningsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author by lrjin, Email 659618618@qq.com, Date on 2018/11/11.
 * PS: Not easy to write code, please indicate.
 */
@RestController
@RequestMapping("com/method")
public class LeveyJenningsController {

    @Autowired
    private LeveyJenningsService leveyJenningsService;

    @GetMapping("/getController")
    public Map<String,Object> getController(){
        Map<String,Object> resultObj = new HashMap<>();
        List<Map<String,Object>> resultMapList = new ArrayList<>();
        List<Kzpcsxxb> resultFaid1 = leveyJenningsService.findByFaid(1);
        List<Kzpcsxxb> resultFaid2 = leveyJenningsService.findByFaid(2);
        for(int i=0;i<resultFaid1.size();i++){
            Map<String,Object> param = new HashMap<>();
            param.put("rq",resultFaid1.get(i).getRi());
            param.put("kzp1",resultFaid1.get(i).getCsz());
            param.put("kzp2",resultFaid2.get(i).getCsz());
            param.put("tishi",msg(resultFaid1.get(i).getCsz(),resultFaid2.get(i).getCsz(),
                    resultFaid1.get(i).getFaid(),resultFaid2.get(i).getFaid(),
                    resultFaid1.get(i).getRi(),0,0));
            resultMapList.add(param);
        }
        resultObj.put("result",resultMapList);
        return resultObj;
    }

    /**
     *
     * @param kzp1
     * @param kzp2
     * @param fn1
     * @param fn2
     * @param date
     * @param upkzp1
     * @param upkzp2
     * @return
     */
    public String msg(double kzp1,double kzp2,int fn1,int fn2,int date, double upkzp1,double upkzp2){
        Kztyzbxxb fa1 = leveyJenningsService.findKztyzbxxbByFaid(fn1);
        Kztyzbxxb fa2 = leveyJenningsService.findKztyzbxxbByFaid(fn2);
        if(kzp1 > fa1.getL3()){
            return "失控";
        }
        if(kzp1 < fa1.getFl3()){
            return "失控";
        }
        if(kzp2 > fa2.getL3()){
            return "失控";
        }
        if(kzp2 < fa2.getFl3()){
            return "失控";
        }
        if(kzp1 >= fa1.getL2() && kzp2 < fa2.getL2()){
            return "警告/在空";
        }
        if(kzp1 < fa1.getL2() && kzp2 >= fa2.getL2()){
            return "在空/警告";
        }
        if(date != 1){
            if(upkzp1 >= fa1.getL2() && upkzp2 < fa2.getL2()){
                if(kzp1 >= fa1.getL2() && kzp2 < fa2.getL2()){
                    return "失控"; //同水平连续 -2s
                }
            }else if(kzp1 < fa1.getL2() && kzp2 >= fa2.getL2()){
                return "在空/警告";
            }
        }

        return "在控";
    }
}
